/*************************************************************************
Purpose: This program examines the input file for any irregularities.
**************************************************************************  
Input:   Reads a file called input_input.
**************************************************************************
Output:  None, just prints warning messages to the screen.
**************************************************************************  
Copyright 2010 Bryan Killett

This file is part of GAIA.

GAIA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GAIA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GAIA. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
//Header file "definitions.hpp" declares many functions, constants, datatypes.
//In order to use the functions declared in definitions.hpp, must link
//with object code that includes functions.cpp.
#include "definitions.hpp"

int main(){

  //1 - Look for duplicate times that occur in a row, print them.  This should
  //    print all duplicate times because the input data has been sorted.
  //2 - Open input cache file as well, search it for binindex values of 
  //    65535 and display the corresponding input line.
  //3 - Display lines when input times (relative to input.epoch) 
  //    match a value in bad_input_times.
  //4 - Display lines when input lon (plus or minus "tolerance")
  //    matches bad_input_lon.
  int choice=4;

  long long input_counter=0;
  char s[max_length];
  double onelat,onelon,oneaccel,onexA,onexB,oneyA,oneyB,onezA,onezB,last_lat,last_lon,last_accel;
  long long onetime,last_time=0;
  int oneascending;
  FILE *in_fp;

  //added for choice 2.
  input_s input,temp_input;
  input_options_s input_options;
  vector <long long> bad_input_indices;
  long long i;
  string filename;//stores JUST the filename of cache file (not the path).
  int matching_cache_found;
  
  //added for choice 3.
  vector <long long> bad_input_times;
  
  //added for choice 4.
  double bad_input_lon;
  
  char* f_return;//Used to stop fgets return value warning.
  
  switch(choice){
    case 1:{
      string input_input(gaiafolder());
      input_input.append("data/input/input_and_orbit_land_and_ocean_edited.txt");
  
      //Open input file for loading.
      in_fp = fopen(input_input.c_str(),"r");
  
      //Warn if input input file doesn't open correctly.
      if (!in_fp) cout << "The input input file, " << input_input
                       << ", failed to open."<<endl;
      else cout << "Loading input input data from file " 
                << input_input << "..."<<endl;

      //Load the first header line separately because it contains no data.
      f_return=fgets(s,max_length,in_fp);
 
      //Load each line of data as a string, parse into numeric values,
      //then figure out if this is a bad point or a good point.  If a good point,
      //write it to the output file.  If bad, ignore it.
      while (fgets(s,max_length,in_fp)!=NULL){
        input_counter++;
        sscanf(s,ORBIT_READ_FORMAT, &onetime, &onelat, &onelon, &oneaccel, &oneascending, &onexA, &onexB, &oneyA, &oneyB, &onezA, &onezB);
        last_time = onetime;
        last_lat = onelat;
        last_lon = onelon;
        last_accel = oneaccel;
        if(onetime==last_time){
          cout<<"input.time["<<input_counter-1<<"] = "<<last_time<<", lat = "<<last_lat<<",lon = "<<last_lon<<", accel = "<<last_accel<<endl;
          cout<<"input.time["<<input_counter<<"] = "<<onetime<<", lat = "<<onelat<<", lon = "<<onelon<<", accel = "<<oneaccel<<endl;
        }
      }
      fclose(in_fp);
      break;
    }//End of case 1.  
    case 2:{
      cout<<"Search for binindices with values of 65535 after binning is finished!  Something fishy is going on here!"<<endl;
      
      temp_input.options.cachefolder = gaiafolder();
      temp_input.options.cachefolder.append("cache/input/");
      filename = "grid-02001000.001000.0001.00049.80090.00000.00360.00-input_filename-input_and_orbit_land_and_ocean_edited.txt";
      string input_input(gaiafolder());
      input_input.append("data/input/input_and_orbit_land_and_ocean_edited.txt");
      
      //First, check to see if a matching cache file exists.
      search_input_cache_for_binindices(temp_input,filename,matching_cache_found);
      if(matching_cache_found != 0) input = temp_input;
      else cout<<"PROBLEM! NO CACHE FILE FOUND!"<<endl;
      
      //Find bad lines in cache file:
      for(i=0;i<(long long)input.binindices.size();i++){
        if(input.binindices.at(i) == 65535) bad_input_indices.push_back(i);
      }

      //Open input file for loading.
      in_fp = fopen(input_input.c_str(),"r");
  
      //Warn if input input file doesn't open correctly.
      if (!in_fp) cout << "The input input file, " << input_input
                       << ", failed to open."<<endl;
      else cout << "Loading input input data from file "
                << input_input << "..."<<endl;
    
      //Load the first header line separately because it contains no data.
      f_return=fgets(s,max_length,in_fp);
 
      while (fgets(s,max_length,in_fp)!=NULL){
        sscanf(s,ORBIT_READ_FORMAT, &onetime, &onelat, &onelon, &oneaccel, &oneascending, &onexA, &onexB, &oneyA, &oneyB, &onezA, &onezB);
        if(count(bad_input_indices.begin(),bad_input_indices.end(),input_counter) != 0) cout<<"Bad line at index "<<input_counter<<":"<<endl<<s<<endl;
        input_counter++;
      }
      fclose(in_fp);
      break;
    }//End of case 2.
    case 3:{
      string input_input(gaiafolder());
      input_input.append("data/input/input_and_orbit_land_and_ocean_edited.txt");
      
      bad_input_times.push_back(162621885);

      //Open input file for loading.
      in_fp = fopen(input_input.c_str(),"r");
  
      //Warn if input input file doesn't open correctly.
      if (!in_fp) cout << "The input input file, " << input_input
                       << ", failed to open."<<endl;
      else cout << "Loading input input data from file "
                << input_input << "..."<<endl;
    
      //Load the first header line separately because it contains no data.
      f_return=fgets(s,max_length,in_fp);
 
      while (fgets(s,max_length,in_fp)!=NULL){
        sscanf(s,ORBIT_READ_FORMAT, &onetime, &onelat, &onelon, &oneaccel, &oneascending, &onexA, &onexB, &oneyA, &oneyB, &onezA, &onezB);
        if(input_counter==0) input.epoch = onetime;
        if(count(bad_input_times.begin(),bad_input_times.end(), onetime - input.epoch) != 0) cout<<"Bad line at index "<<input_counter<<":"<<endl<<s<<endl;
        input_counter++;
      }
      fclose(in_fp);
      break;
    }//End of case 3.
    case 4:{
      string input_input(gaiafolder());
      input_input.append("data/input/input_and_orbit_land_and_ocean_edited.txt");
      
      bad_input_lon = 360.0;

      //Open input file for loading.
      in_fp = fopen(input_input.c_str(),"r");
  
      //Warn if input input file doesn't open correctly.
      if (!in_fp) cout << "The input input file, " << input_input
                       << ", failed to open."<<endl;
      else cout << "Loading input input data from file "
                << input_input << "..."<<endl;
    
      //Load the first header line separately because it contains no data.
      f_return=fgets(s,max_length,in_fp);
 
      while (fgets(s,max_length,in_fp)!=NULL){
        sscanf(s,ORBIT_READ_FORMAT, &onetime, &onelat, &onelon, &oneaccel, &oneascending, &onexA, &onexB, &oneyA, &oneyB, &onezA, &onezB);
        if(input_counter==0) input.epoch = onetime;
        if(fabs(onelon - bad_input_lon) < tolerance) cout<<"Bad line at index "<<input_counter<<":"<<endl<<s<<endl;
        input_counter++;
      }
      fclose(in_fp);
      break;
    }//End of case 4.
  }//End of switch-case-break.

  return 0;
}
